<?php

/**
 * This is the model class for table "{{units}}".
 *
 * The followings are the available columns in table '{{units}}':
 * @property string $unit_id
 * @property string $unit_code
 * @property string $unit_name
 * @property string $unit_nicename
 * @property string $unit_admin
 * @property string $unit_email
 * @property string $unit_tel
 * @property string $unit_url
 * @property string $unit_splash
 * @property string $unit_logo
 * @property string $unit_activation_key
 * @property integer $unit_status
 * @property string $unit_remark
 * @property string $created_at
 * @property string $updated_at
 */
class Unit extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Unit the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{units}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('unit_code, unit_name', 'required'),
			array('unit_status', 'numerical', 'integerOnly'=>true),
			array('unit_code, unit_admin, unit_tel', 'length', 'max'=>20),
			array('unit_name, unit_email, unit_url, unit_splash', 'length', 'max'=>100),
			array('unit_nicename', 'length', 'max'=>50),
			array('unit_logo, unit_remark', 'length', 'max'=>255),
			array('unit_activation_key', 'length', 'max'=>45),
			array('created_at, updated_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('unit_id, unit_code, unit_name, unit_nicename, unit_admin, unit_email, unit_tel, unit_url, unit_splash, unit_logo, unit_activation_key, unit_status, unit_remark, created_at, updated_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'unit_id' => 'Unit',
			'unit_code' => 'Unit Code',
			'unit_name' => 'Unit Name',
			'unit_nicename' => 'Unit Nicename',
			'unit_admin' => 'Unit Admin',
			'unit_email' => 'Unit Email',
			'unit_tel' => 'Unit Tel',
			'unit_url' => 'Unit Url',
			'unit_splash' => 'Unit Splash',
			'unit_logo' => 'Unit Logo',
			'unit_activation_key' => 'Unit Activation Key',
			'unit_status' => 'Unit Status',
			'unit_remark' => 'Unit Remark',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('unit_id',$this->unit_id,true);
		$criteria->compare('unit_code',$this->unit_code,true);
		$criteria->compare('unit_name',$this->unit_name,true);
		$criteria->compare('unit_nicename',$this->unit_nicename,true);
		$criteria->compare('unit_admin',$this->unit_admin,true);
		$criteria->compare('unit_email',$this->unit_email,true);
		$criteria->compare('unit_tel',$this->unit_tel,true);
		$criteria->compare('unit_url',$this->unit_url,true);
		$criteria->compare('unit_splash',$this->unit_splash,true);
		$criteria->compare('unit_logo',$this->unit_logo,true);
		$criteria->compare('unit_activation_key',$this->unit_activation_key,true);
		$criteria->compare('unit_status',$this->unit_status);
		$criteria->compare('unit_remark',$this->unit_remark,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 保存前初始化数据。
	 * @see CActiveRecord#beforeSave()
	 */
	protected function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->unit_status = 1;
			$this->created_at = date('Y-m-d H:i:s');
			$this->updated_at = date('Y-m-d H:i:s');
		}
		else
		{
			$this->updated_at = date('Y-m-d H:i:s');
		}
		return true;
	}
}